package com.iplant.aps.serviceImpl.dao.oms;

import com.iplant.aps.service.po.aps.APSTaskPart;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.base.utils.sql.ShrisJdbcTemplate;
import com.iplant.basic.service.po.bms.BMSEmployee;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
public class OMSOrderDAO extends BaseDAO {

    private static OMSOrderDAO Instance = null;

    private OMSOrderDAO() {
        super(OMSOrder.class);
    }

    public static OMSOrderDAO getInstance() {
        if (Instance == null)
            Instance = new OMSOrderDAO();
        return Instance;
    }


    public Map<Integer, Integer> OMS_GetRepairMap(BMSEmployee wLoginUser, List<Integer> wOrderIDList,
                                                  OutResult<Integer> wErrorCode) {
        Map<Integer, Integer> wResult = new HashMap<>();
        try {
            if (wOrderIDList == null || wOrderIDList.size() <= 0) {
                return wResult;
            }
            String wSQL = StringUtils.Format("SELECT OrderID,count(*) ReworkNum FROM {0}.sfc_workpiece_record where " +
                            "ReworkFlag >0 and OrderID in ({1}) group by OrderID;",
                    DBSchemas.MES.getDBName(), StringUtils.Join(",", wOrderIDList));

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQL, wParamMap);

            for (Map<String, Object> wReader : wQueryResult) {
                wResult.put(StringUtils.parseInt(wReader.get("OrderID")), StringUtils.parseInt(wReader.get("ReworkNum"
                )));
            }
        } catch (Exception ex) {
            log.error(ex.toString());
        }
        return wResult;
    }

    public Map<Integer, Integer> OMS_GetSamplingMap(BMSEmployee wLoginUser, List<Integer> wOrderIDList, OutResult<Integer> wErrorCode) {
        Map<Integer, Integer> wResult = new HashMap<>();
        try {
            if (wOrderIDList == null || wOrderIDList.size() <= 0) {
                return wResult;
            }
            String wSQL = StringUtils.Format("SELECT OrderID,count(*) ReworkNum FROM {0}.sfc_workpiece_record where " +
                            "ReworkFlag >0 and OrderID in ({1}) group by OrderID;",
                    DBSchemas.MES.getDBName(), StringUtils.Join(",", wOrderIDList));

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQL, wParamMap);

            for (Map<String, Object> wReader : wQueryResult) {
                wResult.put(StringUtils.parseInt(wReader.get("OrderID")), StringUtils.parseInt(wReader.get("ReworkNum"
                )));
            }
        } catch (Exception ex) {
            log.error(ex.toString());
        }
        return wResult;
    }
}
